package com.example.woke1;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import cn.xl.xxl.imagecache.ImageLoader;

import com.http.HttpMethods;
import com.zhongcai.online.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Datas_fahuo;
import com.woke.method.Intentnet;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ZhongokActivity extends Activity implements OnClickListener {

	private Intent intent;
	private String orderid;
	private TextView mTname,mTmoney,mTstas,mTtime,mTwho;
	private EditText mTkuaidi,mTid;
	private ImageView mImage;
	private View showview;
	private ImageLoader imageLoader;
	private TextView mTok;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhongok);
		
		imageLoader = new ImageLoader(this);
		intview();
		
	}
	
	@Override
	protected void onDestroy() {
		imageLoader.clearCache();
		imageLoader.clearMemoryCache();
		super.onDestroy();
	}

	private void intview() {
		intent = getIntent();
		orderid = intent.getStringExtra("id");
		mTname = (TextView) findViewById(R.id.zhongok_text_productname);
		mTid = (EditText) findViewById(R.id.zhongok_text_id);
		mTkuaidi = (EditText) findViewById(R.id.zhongok_text_kuandi);
		mTmoney = (TextView) findViewById(R.id.zhongok_text_money);
		mTstas = (TextView) findViewById(R.id.zhongok_text_stats);
		mTtime = (TextView) findViewById(R.id.zhongok_text_time);
		mTwho = (TextView) findViewById(R.id.zhongok_text_who);
		mImage = (ImageView) findViewById(R.id.zhongok_image);
		showview = findViewById(R.id.zhongok_progress_bar);
		findViewById(R.id.zhongok_image_back).setOnClickListener(this);
		mTok = (TextView) findViewById(R.id.zhongok_text_ok);
		mTok.setOnClickListener(this);
		cxtchongzhi();
	}
	
	private void cxtchongzhi() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "orderDetail");
		params.put("id",orderid);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("详情", "充值"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					JSONObject jsonObject = response.getJSONObject("data");
//					newlist.clear();
					if(flag.equals("success")){
						String str_result = jsonObject.getString("data");
						Datas_fahuo data = com.alibaba.fastjson.JSONObject.parseObject(str_result, Datas_fahuo.class);
//						mTname,mTid,mTkuaidi,mTmoney,mTstas,mTtime,mTwho
						mTname.setText(data.getProduct_name());
						imageLoader.DisplayImage("http://"+data.getProduct_img(), mImage, false, false, false,4);
						if(data.getTransport_name().equals("0") || data.getTransport_name().equals("")){
							mTkuaidi.setHint("请输入快递公司名称");
						}else{
							mTkuaidi.setText(data.getTransport_name());
						}
						
						if(data.getTransport_code().equals("0") || data.getTransport_code().equals("")){
							mTid.setHint("请输入快递单号");
						}else{
							mTid.setText(data.getTransport_code());
						}
						mTmoney.setText(data.getMoney()+"元");
						if(data.getStatus().equals("1")){
							mTstas.setText("已付款");
						}if(data.getStatus().equals("2")){
							mTstas.setText("已发货");
//							mTok.setBackgroundColor(getResources().getColor(R.color.usercenterbg));
							mTok.setText("已发货");
							mTok.setClickable(false);
						}if(data.getStatus().equals("3")){
							mTstas.setText("已完成");
							mTok.setText("已完成");
//							mTok.setBackgroundColor(getResources().getColor(R.color.usercenterbg));
							mTok.setClickable(false);
						}
						mTtime.setText(Intentnet.getStrTime(data.getCreatetime()));
						mTwho.setText(data.getName());
//						newlist = (ArrayList<Datas_myzhongc1>) JSONArray.parseArray(str_result, Datas_myzhongc1.class);
//						datalist.addAll(newlist);
//						adapter.notifyDataSetChanged();
					}
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.zhongok_image_back:
			finish();
			break;
			
		case R.id.zhongok_text_ok:
			String kuaidiid = mTid.getText().toString().trim();
			String kuaidigong = mTkuaidi.getText().toString().trim();
			if(null == kuaidigong || kuaidigong.equals("")){
				Toast.makeText(ZhongokActivity.this, "请输入快递公司˾", Toast.LENGTH_SHORT).show();
			}else if(null == kuaidiid || kuaidiid.equals("")){
				Toast.makeText(ZhongokActivity.this, "请输入快递单号", Toast.LENGTH_SHORT).show();
			}else{
				cxtchongzhi1(kuaidiid,kuaidigong);
			}
			break;
	

		default:
			break;
		}
	}
	
	private void cxtchongzhi1(String kuaidiid, String kuaidigong) {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "delivery");
		params.put("id",orderid);
		params.put("transport_name", kuaidigong);
		params.put("transport_code", kuaidiid);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("详情", "充值"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					JSONObject jsonObject = response.getJSONObject("data");
//					newlist.clear();
					if(flag.equals("success")){
						String str_result = jsonObject.getString("data");
						Toast.makeText(ZhongokActivity.this, ""+info, Toast.LENGTH_SHORT).show();
						finish();
					}else{
						Toast.makeText(ZhongokActivity.this, ""+info, Toast.LENGTH_SHORT).show();
					}
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(ZhongokActivity.this, "发货失败!", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				showview.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				showview.setVisibility(View.GONE);
			}
		});
	}
	
}
